home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / QixView.BackModule / QixView.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.9 KB  |  198 lines

  1.  
  2. #import    <stdlib.h>
  3. #import    <math.h>
  4. #import    <appkit/Application.h>
  5. #import    <appkit/Slider.h>
  6. #import    <appkit/Button.h>
  7. #import    <appkit/NXImage.h>
  8. #import    <dpsclient/wraps.h>
  9. #import    "QixView.h"
  10.  
  11. /**********************************************************************/
  12.  
  13. #define    LEFT        ( 100 )
  14. #define    RIGHT        ( 101 )
  15. #define    UP            ( 102 )
  16. #define    DOWN        ( 103 )
  17.  
  18. #define    INITLEN        ( 55 )                //    Initial qix tail length.
  19.  
  20. #define    A_BASE_INC    ( 5 )                //    Default distance to move the
  21.                                         //    "A" point of a qix structure.
  22. #define    B_BASE_INC    ( 8 )                //    Default distance to move the
  23.                                         //    "B" point of a qix structure.
  24.  
  25. /**********************************************************************/
  26.  
  27.  
  28. @implementation QixView
  29.  
  30. /**********************************************************************/
  31.  
  32. - newWindow
  33. {
  34.     [ self resetQix : &head : NO ];
  35.     [ self resetQix : &tail : YES ];
  36.     
  37.     return self;
  38. }
  39.  
  40. /**********************************************************************/
  41.  
  42. - (const char *) windowTitle
  43. {
  44.     return ( const char * ) "Qix Lines";
  45. }
  46.  
  47. /**********************************************************************/
  48.  
  49. - initFrame : ( const NXRect * ) frameRect
  50. {
  51.     [ super initFrame : frameRect ];
  52.     
  53.     [ self setOpaque : YES ];
  54.     [ self setClipping : NO ];
  55.     
  56.     [ self resetQix : &head : NO ];
  57.     [ self resetQix : &tail : YES ];
  58.     
  59.     return self;
  60. }
  61.  
  62. /**********************************************************************/
  63.  
  64. - sizeTo : ( NXCoord ) width : ( NXCoord ) height
  65. {
  66.     [ super sizeTo : width : height ];
  67.     
  68.     [ self resetQix : &head : NO ];
  69.     [ self resetQix : &tail : YES ];
  70.     
  71.     return self;
  72. }
  73.  
  74. /**********************************************************************/
  75.  
  76. - resetQix : ( QIX * ) qix : ( BOOL ) resetControls
  77. {
  78.     if( resetControls == YES )
  79.         tailLen = INITLEN;
  80.     
  81.     qix->pointA.x = bounds.size.width / 3.0;
  82.     qix->pointA.y = bounds.size.height / 3.0;
  83.     qix->pointA.x_dir = RIGHT;
  84.     qix->pointA.y_dir = DOWN;
  85.     qix->pointA.x_inc = A_BASE_INC;
  86.     qix->pointA.y_inc = A_BASE_INC;
  87.     qix->pointA.orig_inc = A_BASE_INC;
  88.     
  89.     qix->pointB.x = bounds.size.width / 2.0;
  90.     qix->pointB.y = bounds.size.height / 2.0;
  91.     qix->pointB.x_dir = LEFT;
  92.     qix->pointB.y_dir = UP;
  93.     qix->pointB.x_inc = B_BASE_INC;
  94.     qix->pointB.y_inc = B_BASE_INC;
  95.     qix->pointB.orig_inc = B_BASE_INC;
  96.     
  97.     return self;
  98. }
  99.  
  100. /**********************************************************************/
  101.     
  102. - setQixPoint : ( MVPOINT * ) point
  103. {
  104.     if( point->x >= bounds.size.width )
  105.     {
  106.         point->x_dir = LEFT;
  107.         point->x_inc = point->orig_inc;
  108.     }
  109.     else if( point->x <= 0 )
  110.         point->x_dir = RIGHT;
  111.     
  112.     if( point->x_dir == RIGHT )
  113.     {
  114.         point->x += point->x_inc;
  115.         point->x_inc -= .009;
  116.     }
  117.     else
  118.     {
  119.         point->x -= point->x_inc;
  120.         point->x_inc += .03;
  121.     }
  122.  
  123.     if( point->y >= bounds.size.height )
  124.     {
  125.         point->y_dir = DOWN;
  126.         point->y_inc = point->orig_inc;
  127.     }
  128.     else if( point->y <= 0 )
  129.         point->y_dir = UP;
  130.         
  131.     if( point->y_dir == UP )
  132.     {
  133.         point->y += point->y_inc;
  134.         point->y_inc -= .009;
  135.     }
  136.     else
  137.     {
  138.         point->y -= point->y_inc;
  139.         point->y_inc += .06;
  140.     }
  141.         
  142.     return self;
  143. }
  144.  
  145. /**********************************************************************/
  146.  
  147. - drawQix : ( QIX ) qix
  148. {
  149.     PSsetlinewidth( 0.5 );
  150.  
  151.     PSmoveto( qix.pointA.x, qix.pointA.y );
  152.     PSlineto( qix.pointB.x, qix.pointB.y );
  153.     PSstroke( );
  154.     
  155.     return self;
  156. }
  157.  
  158. /**********************************************************************/
  159.  
  160. - oneStep
  161. {
  162.     if( tailLen )
  163.         --tailLen;
  164.     else
  165.     {
  166.         PSsetgray( NX_BLACK );
  167.         [ self drawQix : tail ];
  168.         [ self setQixPoint :  &tail.pointA ];
  169.         [ self setQixPoint :  &tail.pointB ];
  170.     }
  171.     
  172.     PSsetgray( NX_WHITE );
  173.     [ self drawQix : head ];
  174.     [ self setQixPoint : &head.pointA ];
  175.     [ self setQixPoint : &head.pointB ];
  176.     
  177.     return self;
  178. }
  179.  
  180. /**********************************************************************/
  181.  
  182. - drawSelf : ( NXRect * ) r : ( int ) count
  183. {     
  184.     if (!r || !count)
  185.         return self;
  186.         
  187.     PSsetgray( NX_BLACK );
  188.     
  189.     NXRectFill( r );
  190.     
  191.     return self;
  192.     
  193. }
  194.  
  195. /**********************************************************************/
  196.  
  197. @end
  198.